package com.woniuxy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.exception.customException.TheAddedHasBeenExist;
import com.woniuxy.system.entity.AssessmentIndicators;
import com.woniuxy.system.entity.DataRecord;
import com.woniuxy.system.entity.IndiType;
import com.woniuxy.system.mapper.AssessmentIndicatorsMapper;
import com.woniuxy.system.mapper.DataRecordMapper;
import com.woniuxy.system.mapper.IndiTypeMapper;
import com.woniuxy.system.service.AssessmentIndicatorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 考核指标 服务实现类
 * </p>
 *
 * @author woniuxy
 * @since 2022-06-01
 */
@Service
@Transactional
public class AssessmentIndicatorsServiceImpl extends ServiceImpl<AssessmentIndicatorsMapper, AssessmentIndicators> implements AssessmentIndicatorsService {
    @Autowired
    DataRecordMapper dataRecordMapper;
    @Autowired
    AssessmentIndicatorsMapper assessmentIndicatorsMapper;
    @Autowired
    IndiTypeMapper indiTypeMapper;

    @Override
    public Page<AssessmentIndicators> getAssessmentIndicators(IPage<AssessmentIndicators> page, Wrapper<AssessmentIndicators> wrapper) {
        return assessmentIndicatorsMapper.getAssessmentIndicators(page,wrapper);
    }

    @Override
    public boolean addAssessmentIndicators(AssessmentIndicators assessmentIndicators,String userid,String AssessmentTypeId) throws TheAddedHasBeenExist {
        LambdaQueryWrapper<AssessmentIndicators> assessmentIndicatorsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        assessmentIndicatorsLambdaQueryWrapper.eq(AssessmentIndicators::getName,assessmentIndicators.getName());
        List<AssessmentIndicators> list = assessmentIndicatorsMapper.selectList(assessmentIndicatorsLambdaQueryWrapper);
        if(list.size()>0){
            throw new TheAddedHasBeenExist();
        }
        DataRecord dataRecord = new DataRecord();
        dataRecord.setCreateTime(new Date());
        dataRecord.setUserId(Long.valueOf(userid));
        dataRecord.setInfo("新建考核指标");
        //添加记录表
        int insert = dataRecordMapper.insert(dataRecord);
        //插入指标表
        assessmentIndicators.setRecordId(dataRecord.getId());
        int insert1 = assessmentIndicatorsMapper.insert(assessmentIndicators);
        IndiType indiType = new IndiType();
        indiType.setTypeId(Long.valueOf(AssessmentTypeId));
        indiType.setIndiId(assessmentIndicators.getId());
        //插入中间表
        int insert2 = indiTypeMapper.insert(indiType);

        if(insert==1&&insert1==1&&insert2==1){
            return  true;
        }
        return false;
    }
}

